#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;


const wregex mcWregexChinesePost(L"\\d+(后)(面)?");

void TextNormalizer::TnReChinesePost(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, mcWregexChinesePost))
	{
		wstring tWstrMatch = tWsmatch[0];			//匹配到的字符串
		wstring tWstrNumber = tWstrMatch.substr(0, tWstrMatch.length()-1);	//匹配到的字符串
		wstring tWstrUnit = tWstrMatch.substr(tWstrMatch.length()-1);		//数字后面的单位
		
		if(tWstrUnit != L"后") {
			tWstrNumber = tWstrMatch.substr(0, tWstrMatch.length()-2);
			tWstrUnit = tWstrMatch.substr(tWstrMatch.length()-2);		//数字后面的单位
		}


		//两位数才需要变，比如00后、90后
		RepleceMethodEnum tReplaceMethod = (fcInLanguage==LANG_EN) ? RME_en_negative_decimal : RME_zh_negative_decimal;

		if(tWstrNumber.length() == 2  &&  tWstrUnit.length() == 1) {
			tReplaceMethod = (fcInLanguage==LANG_EN) ? RME_en_telephone : RME_zh_digit_yi1;
		}

		string tStrReplace = ReplaceMethod(vector<string>{wstring2utf8string(tWstrNumber)}, tReplaceMethod) + " " + wstring2utf8string(tWstrUnit);

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}



const wregex mcWregexEnglishPost(L"(Post|post)-\\d+s?");

void TextNormalizer::TnReEnglishPost(wstring* fpInWsentence)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, mcWregexEnglishPost))
	{
		const wstring tMatch = tWsmatch[0];
		const wchar_t* tp = tMatch.c_str();

		wstring tWstrNumber;
		for(int i=0; i<tMatch.length(); i++) {
			if(tp[i] >= L'0' && tp[i] <= L'9') {
				tWstrNumber += tp[i];
			}
			else {
				if(tWstrNumber != L"") {
					break;
				}
			}
		}

		//00后:the post-two thousand's generation。90后:the post-ninety's generation。80后：the generation after 80s。
		wstring tWstrReplace = tMatch;
		string tReplaceNumber = ReplaceMethod(vector<string> {wstring2utf8string(tWstrNumber)}, RME_en_negative_decimal) + "'";
		if(tWstrNumber == L"00") {
			tReplaceNumber = " two thousand'";
		}

		WstrReplace(tWstrReplace, wstring2utf8string(tWstrNumber), tReplaceNumber);
		WstrReplace(tWstrReplace, "-", " ");

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), tWstrReplace);
	}
}

//比如"born between 1966 and 1973"
const wregex mcWregexEnNumAndNum(L"\\d+\\s+(and)\\s+\\d+");

void TextNormalizer::TnReEnNumAndNum(wstring* fpInWsentence)
{
	wsmatch tWsmatch;

	while (regex_search(*fpInWsentence, tWsmatch, mcWregexEnNumAndNum))
	{
		//提取数字前面的字符串
		const vector<wstring> tcEnVecStrLastWords = GetWstrPositionLastWords(*fpInWsentence, tWsmatch.position(0), 3, LANG_EN);

		bool tbMaybeYear = false;
		for(const wstring tcWord : tcEnVecStrLastWords) {
			string tStrWord = wstring2utf8string(tcWord);
			transform(tStrWord.begin(), tStrWord.end(), tStrWord.begin(), ::tolower);		//大写转小写
			if(tStrWord == "born") {
				tbMaybeYear = true;
				break;
			}
		}

		string tStrMatch = wstring2utf8string(tWsmatch[0]);
		StrReplace(tStrMatch, " ", "");
		StrReplace(tStrMatch, "and", "|");
		const vector<string> tItemNumbers = StrSplit(tStrMatch, "|");

		string tStrReplace = "";
		if(tbMaybeYear && tItemNumbers.size()==2 && tItemNumbers[0].length()==4 && tItemNumbers[1].length()==4) {
			tStrReplace += ReplaceMethod(vector<string> {tItemNumbers[0]}, RME_en_year);
			tStrReplace += " and ";
			tStrReplace += ReplaceMethod(vector<string> {tItemNumbers[1]}, RME_en_year);
			tStrReplace += " ";
		}
		else {
			tStrReplace += tItemNumbers[0] + " AnD " + tItemNumbers[1];
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace) );
	}
}
